#include "sys_main.h"
#include "stdio.h"
#include "sys_app.h"
#include "input_dev.h"
#include "task_manager.h"
#include "esp_log.h"

#define TAG "SYS_MAIN_UI"

LV_FONT_DECLARE(arial_18);

// 事件回调函数
static void screen_event_cb(lv_event_t* e) {
    lv_dir_t dir = lv_indev_get_gesture_dir(lv_indev_get_act());
    if(dir == LV_DIR_TOP) {
        printf("dir bottom\n");
        sys_main_view_exit(MAIN_VIEW);
        sys_main_view_init(MENU_VIEW);
    }
}

lv_obj_t* sys_main_interface_ui_init(void)
{
    lv_obj_t * obj = lv_obj_create(NULL);
    lv_obj_set_style_bg_color(obj, lv_color_hex(0x000000), 0);
    if (obj == NULL) {
        LV_LOG_ERROR("Failed to create main interface object");
        return NULL;
    }

    // 2. 时间区域布局
    lv_obj_t * time_panel = lv_obj_create(obj);
    lv_obj_clear_flag(time_panel, LV_OBJ_FLAG_SCROLLABLE);
    lv_obj_set_size(time_panel, 330, 70);
    lv_obj_set_style_radius(time_panel, 20, 0);
    lv_obj_set_style_border_opa(time_panel, LV_OPA_0, 0);
    lv_obj_set_style_bg_color(time_panel, lv_color_hex(0xFFC0CB), 0);
    lv_obj_align(time_panel, LV_ALIGN_TOP_LEFT, -20, 0);

    lv_obj_t * time_label = lv_label_create(time_panel);
    lv_label_set_text(time_label, "20:42:48");
    lv_obj_set_style_text_font(time_label, &lv_font_montserrat_48, 0);
    lv_obj_set_style_text_color(time_label, lv_color_hex(0x00d3e9), 0);
    lv_obj_align(time_label, LV_ALIGN_CENTER, 0, 0);

    lv_obj_add_event_cb(obj, screen_event_cb, LV_EVENT_GESTURE, NULL);
    lv_obj_clear_flag(obj, LV_OBJ_FLAG_GESTURE_BUBBLE); 
    return obj;
}

static void audio_btn_cb(lv_event_t * e) {
    lv_event_code_t code = lv_event_get_code(e);
    if (code == LV_EVENT_CLICKED)
    {
        if (sd_card_get_state()) {
            sys_app_entry(SYS_APP_LCMUSIC);
        } else {
            sd_sdiofs_mount();
            if (sd_card_get_state()) {
                sys_app_entry(SYS_APP_LCMUSIC);
            }
        }
        printf("Audio button clicked\n");
    }
}

static void video_btn_cb(lv_event_t * e) {
    lv_event_code_t code = lv_event_get_code(e);
    if (code == LV_EVENT_CLICKED)
    {
        sys_app_entry(SYS_APP_RECORD);
        printf("Video button clicked\n");
    }
}

static void bluetooth_btn_cb(lv_event_t * e) {
    lv_event_code_t code = lv_event_get_code(e);
    if (code == LV_EVENT_CLICKED)
    {
        printf("Bluetooth button clicked\n");
    }
}

static void wifi_btn_cb(lv_event_t * e) {
    lv_event_code_t code = lv_event_get_code(e);
    if (code == LV_EVENT_CLICKED)
    {
        printf("WiFi button clicked\n");
    }
}

static void settings_btn_cb(lv_event_t * e) {
    lv_event_code_t code = lv_event_get_code(e);
    if (code == LV_EVENT_CLICKED)
    {
        printf("Settings button clicked\n");
    }
}

static void home_btn_cb(lv_event_t * e) {
    lv_event_code_t code = lv_event_get_code(e);
    if (code == LV_EVENT_CLICKED)
    {
        printf("Home button clicked\n");
        sys_main_view_exit(MENU_VIEW);
        sys_main_view_init(MAIN_VIEW);
    }
}

static const lv_event_cb_t btn_cbs[] = {
    audio_btn_cb,
    video_btn_cb,
    bluetooth_btn_cb,
    wifi_btn_cb,
    settings_btn_cb,
    home_btn_cb
};

lv_obj_t* sys_main_lvgl_register(void)
{
    const lv_coord_t btn_positions[6][2] = {
        {28, 34}, {125, 34}, {222, 34},
        {28, 137}, {125, 137}, {222, 137}
    };

    const char* symbols[] = {
        LV_SYMBOL_AUDIO,
        LV_SYMBOL_BELL,
        LV_SYMBOL_BLUETOOTH,
        LV_SYMBOL_WIFI,
        LV_SYMBOL_SETTINGS,
        LV_SYMBOL_HOME
    };
 
    static lv_style_t style_btn;
    static lv_style_t style_label;
    static lv_obj_t *lv_obj = NULL;

    lv_obj = lv_obj_create(NULL);
    if (lv_obj == NULL) {
        LV_LOG_ERROR("Failed to create main object");
        return NULL;
    }
    lv_obj_clear_flag(lv_obj, LV_OBJ_FLAG_SCROLLABLE);

    lv_obj_t* main_bg = lv_img_create(lv_obj);
    lv_obj_set_style_bg_opa(main_bg, LV_OPA_COVER, 0);
    lv_obj_set_style_bg_color(main_bg, lv_color_hex(0x000000), 0);
    //lv_obj_img_set_src(main_bg, &img_bg_main);
    lv_obj_set_size(main_bg, LV_HOR_RES, LV_VER_RES);
    lv_obj_set_pos(main_bg, 0, 0);
    
    lv_style_init(&style_btn);
    lv_style_set_width(&style_btn, 70);     
    lv_style_set_height(&style_btn, 70); 
    lv_style_set_radius(&style_btn, 5);
    lv_style_set_bg_opa(&style_btn, LV_OPA_TRANSP);
    lv_style_set_border_opa(&style_btn, LV_OPA_TRANSP);
    lv_style_set_shadow_opa(&style_btn, LV_OPA_TRANSP);

    lv_style_init(&style_label);
    lv_style_set_text_font(&style_label, &lv_font_montserrat_48);
    lv_style_set_align(&style_label, LV_ALIGN_CENTER);
    
    for (int i = 0; i < 6; i++) {
        lv_obj_t *menu_btn = lv_btn_create(main_bg);
        if (menu_btn == NULL) {
            LV_LOG_ERROR("Failed to create menu button %d", i);
            continue;
        }
        lv_obj_set_pos(menu_btn, btn_positions[i][0], btn_positions[i][1]);
        lv_obj_add_style(menu_btn, &style_btn, 0);
        lv_obj_add_event_cb(menu_btn, btn_cbs[i], LV_EVENT_CLICKED, NULL);

        lv_obj_t *label = lv_label_create(menu_btn);
        if (label == NULL) {
            LV_LOG_ERROR("Failed to create label for menu button %d", i);
            continue;
        }
        lv_label_set_text(label, symbols[i]);
        lv_obj_add_style(label, &style_label, 0);
    }

    return lv_obj;
}

static const ui_key_map_t common_keymap[] = {
    { KEY_POWER,    KEY_TYPE_SHORT_UP,      1,          0},
    { KEY_RESERVED,     0,  0,  0 },
};

static int sys_main_view_proc(uint8_t view_id, uint8_t msg_id, uint32_t ui_event)
{
	ESP_LOGI(TAG, "view_id %d msg_id %d ui_event %lu\n", view_id, msg_id, ui_event);

	switch (msg_id) {
        case MSG_VIEW_CREATE:
            if(view_id == MAIN_VIEW)
            {
                ui_manager_load_acr_anim(MAIN_VIEW, LV_SCR_LOAD_ANIM_MOVE_BOTTOM, 200, 0, false);
            }else if(view_id == MENU_VIEW)
            {
                ui_manager_load_acr_anim(MENU_VIEW, LV_SCR_LOAD_ANIM_MOVE_TOP, 200, 0, false);
            }
            
        break;

        case MSG_VIEW_DELETE:
            if(view_id == MAIN_VIEW)
            {
  
            }else if(view_id == MENU_VIEW)
            {

            }
        break;
	
        default:
        break;
	}
	return 0;
}

void sys_main_view_init(uint8_t view_id)
{
    ui_view_info_t view_info;

	memset(&view_info, 0, sizeof(ui_view_info_t));

	view_info.view_proc = sys_main_view_proc;
	view_info.view_key_map = common_keymap;
	view_info.view_get_state = NULL;
	view_info.order = 0;
	view_info.task_name = MAIN_TASK_NAME;

	ui_view_create(view_id, &view_info);
}

void sys_main_view_exit(uint8_t view_id)
{
	ui_view_delete(view_id);
}
